package com.letsiot.applications.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.letsiot.applications.entity.SysMessage;
import com.letsiot.applications.service.SysMessageService;

@RestController
@RequestMapping("/sysMessage")
@Validated
public class SysMessageController {

    @Autowired
    private SysMessageService sysMessageService;

    @PostMapping("save")
    public int save(@RequestBody SysMessage entity) {
        return sysMessageService.save(entity);
    }

    @DeleteMapping("delete")
    public int delete(Long id) {
        return sysMessageService.delete(id);
    }

    @PutMapping("update")
    public int update(@RequestBody SysMessage entity) {
        return sysMessageService.update(entity);
    }

    @GetMapping("findById")
    public SysMessage findById(Long id) {
        return sysMessageService.findById(id);
    }

    @GetMapping("getAll")
    public List<SysMessage> getAll() {
        return sysMessageService.getAll();
    }

    @GetMapping("listMessage")
    public Page<SysMessage> listMessage(String messageType, String isread, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        SysMessage sysMessage = new SysMessage();
        sysMessage.setMessageType(messageType);
        sysMessage.setIsread(isread);
        return sysMessageService.listMessage(sysMessage, pageable);
    }

    @GetMapping("countByMessageTypeAndModule")
    public int countByMessageTypeAndModule(String messageType, String moduleCode, String isread) {
        return sysMessageService.countByMessageTypeAndModule(messageType, moduleCode, isread);
    }

    @GetMapping("findWarningMessageCode")
    public SysMessage findWarningMessageCode(String messageCode) {
        return sysMessageService.findWarningMessageCode(messageCode);
    }
}